package com.itheima.tliasmanagementspringboot.controller;

import com.itheima.tliasmanagementspringboot.annotation.Log;
import com.itheima.tliasmanagementspringboot.common.R;
import com.itheima.tliasmanagementspringboot.utils.AliYunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 处理文件上传
 */
@RestController
@Slf4j
public class UploadController {
    @Autowired
    AliYunOSSUtils aliYunOSSUtils;

/*    @PostMapping("/upload")
    public R upload(String username, Integer age, MultipartFile image) throws IOException {
        log.info("userename: {}, age: {}, image: {}", username, age, image );

        File dir = new File("d:/study/image");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        //获取原始文件名
        String originalFilename = image.getOriginalFilename();

        //截取原始文件名的后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        //生成唯一的UUID文件名
        String uuidFilename = UUID.randomUUID().toString() + suffix;

        //将临时文件转移到指定位置(相当于保存)
        image.transferTo(new File(dir, uuidFilename));
        return R.ok();
    }*/

    @PostMapping("/upload")
    @Log
    public R upload(String username, Integer age, MultipartFile image){
        String url = aliYunOSSUtils.upload2OSS(image);
        return R.ok(url);
    }
}
